#!/usr/bin/python

"""
    ******************************************
    This program requires Python 2.5 or later.
    ******************************************      

	File:		papa-bot.py
	
	Class:		CMPS 140 Winter 2011
	Project:	Papa Bot
	Date:		03/10/11
	Authors:	Jason Erickson
	            Morgan Davis
	            DJ Visbal
	            Constantine Perepelitsa
	            Jae Kim
	            Huu Huynh
"""

from dm import DM
from db import DB
from nlu import NLU
from nlg import NLG
from user import User
import sys
import getopt

class PapaBot(object):
    
    def __init__(self):
        self.debug_mode = False
        self.user = None # parse_args() uses this
        db = DB(self.debug_mode)
        self.db = db # parse_args() uses this
        self.parse_args()
        nlu = NLU(self.debug_mode,db)
        nlg = NLG(self.debug_mode,db)
        dm = DM(self.debug_mode,nlu,db,nlg,self.user)
        # fire up the loopage
        dm.start_state_loop()
	
    def parse_args(self):
        try:
            opts, args = getopt.getopt(sys.argv[1:], 'du:', ['debug', 'user='])
        except getopt.GetoptError, e:
            print >> sys.stderr, str(e)
            exit(1)
        uname = None
        for o, a in opts:
            if o == '-d' or o == '--debug':
                self.debug_mode = True
            elif o == '-u' or o == '--user':
                uname = a   # keep this for debug output
                self.user = User(self.db,a)
    
        if self.debug_mode:
            print >> sys.stderr, "(DEBUG MODE)"
            # user tried to identify
            if uname is not None:
                if self.user is None:
                    print >> sys.stderr,\
                             "(UNRECOGNIZED USER: \"%s\")" % uname
                else:
                    print >> sys.stderr,\
                             "(USER: \"%s\")" % uname
            # user did not try to identify
            else:
                print >> sys.stderr, "(NO USER)"

if __name__ == "__main__":
    """Papa-bot starts here"""
    pb = PapaBot()
